package cc.ycws.web.controller;

import cc.ycws.core.domain.Count;
import cc.ycws.core.domain.Location;
import cc.ycws.core.domain.Uploadfile;
import cc.ycws.core.service.CountService;
import cc.ycws.core.service.LocationService;
import cc.ycws.core.service.UploadfileService;
import cc.ycws.core.util.DataUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/u"})
public class UploadController {

    Logger logger = Logger.getLogger(UploadController.class);
    @Resource
    UploadfileService uploadfileService;
    @Resource
    CountService countService;
    @Resource
    LocationService locationService;

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    public String add(@RequestParam(value = "filepath", required = false) MultipartFile file, HttpServletRequest request, Uploadfile uploadfile, ModelMap modelMap) {
        String uploadImg = request.getParameter("uploadImg");
        String nikname = request.getParameter("nikname");
        String moblie = request.getParameter("moblie");
        String model = request.getParameter("model");
        String address = request.getParameter("address");
        String dong = request.getParameter("dong");
        String openid = request.getParameter("openid");
        String roomandnumber = request.getParameter("roomandnumber");
        String unitbuilding = request.getParameter("unitbuilding");
        Location list = this.locationService.moeny(address);
        uploadfile.setUserid(openid);
        Uploadfile uploadfile2 = this.uploadfileService.userid(uploadfile);
        if (uploadfile2 != null) {
            this.logger.info("修改" + openid);
            uploadfile2.setYid(uploadfile2.getYid());
            uploadfile2.setYfilename(uploadImg);
            uploadfile2.setYname(nikname);
            uploadfile2.setYphone(moblie);
            uploadfile2.setYowner(model);
            uploadfile2.setYaddress(address);
            uploadfile2.setYdetailed(dong);
            uploadfile2.setCreatetime(DataUtil.data1());
            uploadfile2.setUpdatetime(DataUtil.data1());
            uploadfile2.setUserid(openid);
            uploadfile2.setRoomandnumber(roomandnumber);
            uploadfile2.setUnitbuilding(unitbuilding);
            if ((list == null) || (list.getMoeny().equals("0"))) {
                uploadfile2.setYindex("1");
                uploadfile2.setIdnumber(DataUtil.data3(1));
                this.uploadfileService.update(uploadfile2);
                return "redirect:/p/success?userid=" + openid + "&yphone=" + moblie;
            }
            uploadfile2.setYindex("0");
            uploadfile2.setIdnumber(DataUtil.data3(0));
            this.uploadfileService.update(uploadfile2);
            modelMap.addAttribute("moblie", openid);
            modelMap.addAttribute("yphone", moblie);
            return "csh/homePageNo";
        }
        this.logger.info("添加" + openid);
        uploadfile.setYfilename(uploadImg);
        uploadfile.setYname(nikname);
        uploadfile.setYphone(moblie);
        uploadfile.setYowner(model);
        uploadfile.setYaddress(address);
        uploadfile.setYdetailed(dong);
        uploadfile.setCreatetime(DataUtil.data1());
        uploadfile.setUpdatetime(DataUtil.data1());
        uploadfile.setUserid(openid);
        uploadfile.setRoomandnumber(roomandnumber);
        uploadfile.setUnitbuilding(unitbuilding);
        if ((list == null) || (list.getMoeny().equals("0"))) {
            uploadfile.setYindex("1");
            uploadfile.setIdnumber(DataUtil.data3(1));
            this.uploadfileService.insert(uploadfile);
            return "redirect:/p/success?userid=" + openid + "&yphone=" + moblie;
        }
        uploadfile.setYindex("0");
        uploadfile.setIdnumber(DataUtil.data3(0));
        this.uploadfileService.insert(uploadfile);
        modelMap.addAttribute("moblie", openid);
        modelMap.addAttribute("yphone", moblie);
        return "csh/homePageNo";
    }

    /**
     * 查询彩之云openid是否存在
     *
     * @param uploadfile
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/doesitExist"}, method = {RequestMethod.GET})
    public String doesitExist(Uploadfile uploadfile) {
        Map<String, Object> map = new HashMap<String, Object>();
        uploadfile.setUserid(uploadfile.getUserid());
        try {
            Uploadfile user = this.uploadfileService.userid(uploadfile);
            if (user != null) {
                map.put("code", "1");
                JSONObject jsonObject = new JSONObject(map);
                return jsonObject.toString();
            }
            map.put("code", "0");
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        } catch (Exception e) {
            map.put("code", "500");
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        }
    }

    @RequestMapping(value = {"/home"}, method = {RequestMethod.GET})
    public String home(Uploadfile uploadfile, ModelMap modelMap) {
        this.logger.info("进入个人中心:openid=" + uploadfile.getUserid());
        uploadfile.setUserid(uploadfile.getUserid());
        Uploadfile user = this.uploadfileService.userid(uploadfile);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("userid", user.getUserid());
        modelMap.addAttribute("yphone", user.getYphone());
        modelMap.addAttribute("yfilename", user.getYfilename());
        return "csh/my";
    }

    @ResponseBody
    @RequestMapping(value = {"/selectMsg"}, method = {RequestMethod.GET})
    public String selectMsg(String id, Uploadfile uploadfile) {
        uploadfile.setUserid(id);
        try {
            Uploadfile user = this.uploadfileService.userid(uploadfile);
            return Integer.toString(user.getMessageset().intValue());
        } catch (Exception e) {
            this.logger.info(e);
        }
        return "0";
    }

    @RequestMapping(value = {"/msgSet"}, method = {RequestMethod.POST})
    public String msgSet(String userid, int messageset, Uploadfile uploadfile) {
        uploadfile.setUserid(userid);
        uploadfile.setMessageset(Integer.valueOf(messageset));
        try {
            this.uploadfileService.msgSet(uploadfile);
        } catch (Exception e) {
            this.logger.info(e);
        }
        return "csh/my";
    }

    @RequestMapping({"/JumpBuy"})
    public String JumpBuy(String userid, String yphone, ModelMap modelMap) {
        modelMap.addAttribute("moblie", userid);
        modelMap.addAttribute("yphone", yphone);
        return "csh/buy";
    }

    @RequestMapping({"/JumpRightInformation"})
    public String JumpRightInformation(Uploadfile uploadfile, String userid, ModelMap modelMap) {
        uploadfile.setUserid(userid);
        modelMap.addAttribute("moblie", userid);
        Uploadfile upload = this.uploadfileService.userid(uploadfile);
        modelMap.addAttribute("upload", upload);
        return "csh/rightInformation";
    }

    @RequestMapping({"/JumpsystemMassage"})
    public String JumpsystemMassage(String userid, ModelMap modelMap) {
        modelMap.addAttribute("moblie", userid);
        return "csh/systemMassage";
    }

    @RequestMapping({"/Jumpdoor"})
    public String Jumpdoor(String userid, ModelMap modelMap, Uploadfile uploadfile) {
        uploadfile.setUserid(userid);
        Uploadfile upload = this.uploadfileService.userid(uploadfile);
        List<Count> list = this.countService.Uopendoor(upload.getYid().intValue());
        modelMap.addAttribute("moblie", userid);
        modelMap.addAttribute("list", list);
        return "csh/door";
    }

    @RequestMapping({"/JumpaddressSearch1"})
    public String JumpaddressSearch1(String nickname, String mobile, String openid, String img, ModelMap modelMap)
            throws Exception {
        String nickname1 = new String(nickname.getBytes("ISO-8859-1"), "utf-8");
        String mobile1 = new String(mobile.getBytes("ISO-8859-1"), "utf-8");
        String openid1 = new String(openid.getBytes("ISO-8859-1"), "utf-8");
        this.logger.info("nickname:" + nickname1);
        this.logger.info("mobile:" + mobile1);
        this.logger.info("openid:" + openid1);
        this.logger.info("img:" + img);
        modelMap.addAttribute("nickname", nickname1);
        modelMap.addAttribute("mobile", mobile1);
        modelMap.addAttribute("openid", openid1);
        modelMap.addAttribute("uploadImg", img);
        return "csh/addressSearch1";
    }

    @RequestMapping({"/JumpuserInformation"})
    public String JumpuserInformation(String address, String dong, String nickname, String mobile, String openid, String uploadImg, ModelMap modelMap)
            throws Exception {
        String address1 = new String(address.getBytes("ISO-8859-1"), "utf-8");
        String dong1 = new String(dong.getBytes("ISO-8859-1"), "utf-8");
        String nickname1 = new String(nickname.getBytes("ISO-8859-1"), "utf-8");
        String mobile1 = new String(mobile.getBytes("ISO-8859-1"), "utf-8");
        String openid1 = new String(openid.getBytes("ISO-8859-1"), "utf-8");
        modelMap.addAttribute("nickname", nickname1);
        modelMap.addAttribute("mobile", mobile1);
        modelMap.addAttribute("address", address1);
        modelMap.addAttribute("dong", dong1);
        modelMap.addAttribute("openid", openid1);
        modelMap.addAttribute("uploadImg", uploadImg);
        this.logger.info("uploadImg:" + uploadImg);
        return "csh/userInformation";
    }

    @RequestMapping({"/JumpCardNumber"})
    public String JumpCardNumber(String openid, String mobile, String community_name, ModelMap modelMap)
            throws Exception {
        String community_name1 = new String(community_name.getBytes("ISO-8859-1"), "utf-8");
        modelMap.addAttribute("openid", openid);
        modelMap.addAttribute("mobile", mobile);
        modelMap.addAttribute("community_name", community_name1);
        return "csh/CardNumber";
    }
}
